package com.team7.home.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.team7.common.entity.Article;
import com.team7.common.entity.BottomArticle;
import com.team7.common.vo.BottomArticleVo;
import com.team7.common.vo.CommonResult;
import com.team7.home.dao.ArticleDao;
import com.team7.home.dao.BottomArticleDao;
import com.team7.home.service.BottomArticleService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: team7-object-master
 * @description:
 * @author: 王金涛
 * @create: 2021-10-19 14:48
 **/
@Service
public class BottomArticleServiceImpl extends ServiceImpl<BottomArticleDao, BottomArticle> implements BottomArticleService {

    @Resource
    private BottomArticleDao bottomArticleDao;

    @Resource
    private ArticleDao articleDao;

    /**
     * 分页查询显示所有的文章
     * @param page
     * @param limit
     * @param bottomArticleVo
     * @return
     */
    @Override
    public CommonResult getAllBottomArticle(Integer page, Integer limit, BottomArticleVo bottomArticleVo) {
        Page<BottomArticle> articlePage = new Page(page, limit);

        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(bottomArticleVo.getName())){
            wrapper.eq("name",bottomArticleVo.getName());
        }

        if (bottomArticleVo.getIsDisable()!=null){
            wrapper.eq("is_disable",bottomArticleVo.getIsDisable());
        }
        wrapper.eq("is_deleted",0);
        wrapper.eq("parent_id","0");
        QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("is_deleted",0);
        //获取所有数据
        IPage<BottomArticle> bottomArticles = bottomArticleDao.selectPage(articlePage,wrapper);

        List<BottomArticle> records = bottomArticles.getRecords();
        //
        List<BottomArticle> articleList = bottomArticleDao.selectList(wrapper1);

        for (BottomArticle bottomArticle : records){
            bottomArticle.setChildren(getAllChildrenArticle(articleList,bottomArticle.getId()));
        }
        return new CommonResult(2000,"查询成功",bottomArticles);
    }

    /**
     * 获得所有学生文章
     * @param bottomArticles
     * @param parentId
     * @return
     */
    private List<BottomArticle> getAllChildrenArticle(List<BottomArticle> bottomArticles, String parentId) {
        ArrayList<BottomArticle> childrenlist = new ArrayList();

        for (BottomArticle children : bottomArticles) {
            if (children.getParentId().equals(parentId)){
                childrenlist.add(children);
            }
        }

        for (BottomArticle child:childrenlist){
            child.setChildren(getAllChildrenArticle(bottomArticles,child.getId()));
        }
        return childrenlist;
    }

    @Override
    @Transactional
    public CommonResult addBottomArticle(BottomArticleVo bottomArticleVo) {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        String name = bottomArticleVo.getName();
        wrapper.eq("name",name);
        wrapper.eq("is_deleted",0);
        BottomArticle bottomArticle = bottomArticleDao.selectOne(wrapper);
        if (bottomArticle!=null){
            return new CommonResult(5000,"添加失败，名称已存在",null);
        }else {
            BottomArticle article = new BottomArticle();
            article.setName(bottomArticleVo.getName());
            article.setGmtModified(LocalDateTime.now());
            article.setGmtCreate(LocalDateTime.now());
            article.setParentId(bottomArticleVo.getParentId());
            article.setIsDeleted(0);
            article.setIsDisable(0);
            int insert = bottomArticleDao.insert(article);
            if (!"0".equals(bottomArticleVo.getParentId()) && insert>0){
                QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
                wrapper1.eq("name",bottomArticleVo.getName());
                String id = bottomArticleDao.selectOne(wrapper1).getId();
                Article article1 = new Article();
                article1.setGmtModified(LocalDateTime.now());
                article1.setArtTitle(bottomArticleVo.getName());
                article1.setNavId(id);
                article1.setGmtCreate(LocalDateTime.now());
                article1.setIsDeleted(0);
                int insert1 = articleDao.insert(article1);
                if (insert1>0){
                    return new CommonResult(2000,"添加成功",null);
                }
            }
            if (insert>0){
                return new CommonResult(2000,"添加成功",null);
            }else {
                return new CommonResult(5000,"添加失败",null);
            }
        }
    }

    @Override
    public CommonResult editBottomArticle(BottomArticleVo bottomArticleVo) {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        String name = bottomArticleVo.getName();
        wrapper.eq("name",name);
        wrapper.eq("is_deleted",0);
        BottomArticle bottomArticle = bottomArticleDao.selectOne(wrapper);
        if (bottomArticle!=null){
            return  new CommonResult(5000,"更新失败，名称已存在",null);
        }else {
            BottomArticle article = new BottomArticle();
            article.setId(bottomArticleVo.getId());
            article.setName(bottomArticleVo.getName());
            article.setGmtModified(LocalDateTime.now());
            int i = bottomArticleDao.updateById(article);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return new CommonResult(5000,"更新失败",null);
            }
        }
    }

    @Override
    public CommonResult deleteBottomArticle(String id) {
        BottomArticle bottomArticle = new BottomArticle();
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        BottomArticle bottomArticle1 = bottomArticleDao.selectOne(wrapper);
        if (bottomArticle1.getParentId().equals("0")){
            QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",id);
            wrapper1.or();
            wrapper1.eq("id",id);
            bottomArticle.setIsDeleted(1);
            bottomArticleDao.update(bottomArticle, wrapper1);
            return new CommonResult(2000,"删除成功", null);
        }else {
            bottomArticle.setId(id);
            bottomArticle.setIsDisable(1);
            int i = bottomArticleDao.updateById(bottomArticle);
            if (i>0){
                return new CommonResult(2000,"删除成功",null);
            }else {
                return new CommonResult(5000,"删除失败",null);
            }
        }
    }

    @Override
    public CommonResult changeStatus(String id, Integer isDisable) {
        BottomArticle bottomArticle = new BottomArticle();
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        BottomArticle bottomArticle1 = bottomArticleDao.selectOne(wrapper);
        if (bottomArticle1.getParentId().equals("0")){
            QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
            wrapper1.eq("parent_id",id);
            wrapper1.or();
            wrapper1.eq("id",id);
            bottomArticle.setIsDisable(isDisable);
            bottomArticleDao.update(bottomArticle,wrapper1);
            return new CommonResult(2000,"更新成功",null);
        }else {
            bottomArticle.setId(id);
            bottomArticle.setIsDisable(isDisable);
            int i = bottomArticleDao.updateById(bottomArticle);
            if (i>0){
                return new CommonResult(2000,"更新成功",null);
            }else {
                return new CommonResult(2000,"更新失败",null);
            }
        }
    }

    @Override
    public CommonResult getQianAllBottomArticle() {
        QueryWrapper<BottomArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("is_disable",0);
        wrapper.eq("is_deleted",0);
        wrapper.eq("parent_id","0");
        //获取未删除启用的一级底部文章
        List<BottomArticle> bottomArticles = bottomArticleDao.selectList(wrapper);
        //获取所有文章
        QueryWrapper<BottomArticle> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("is_disable",0);
        wrapper1.eq("is_deleted",0);
        List<BottomArticle> bottomArticles1 = bottomArticleDao.selectList(wrapper1);
        for (BottomArticle bottomArticle: bottomArticles){
            bottomArticle.setChildren(getAllChildrenArticle(bottomArticles1,bottomArticle.getId()));
        }
        return new CommonResult(2000,"查询成功",bottomArticles);
    }
}
